package net.weitalk.newfeatures;

import java.time.LocalDate;
import java.time.Month;

/**
 * 使用LocalDate之后，Month为枚举类型，switch表达式不用default分支即可穷举所有情景，
 * 如果出现第13个月，JDK更新之后，编译会报错。
 */
public class DaysInMonthSwitchExpressionLocalDate {
    private static boolean isLeapYear(int year) {
        return (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0));
    }

    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        Month month = today.getMonth();
        int daysInMonth = switch (month) {
            case JANUARY,
                    MARCH,
                    MAY,
                    JULY,
                    AUGUST,
                    OCTOBER,
                    DECEMBER -> 31;
            case APRIL,
                    JUNE,
                    SEPTEMBER,
                    NOVEMBER -> 30;
            case FEBRUARY -> {
                if (isLeapYear(today.getYear())) {
                    yield 29;
                } else {
                    yield 28;
                }
            }
        };
        System.out.println("There are " + daysInMonth + " days in this month.");
    }
}
